package com.bsb.biz.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.bsb.base.common.config.Const;
import com.bsb.base.model.SysOrganization;
import com.bsb.base.model.SysOrganizationExample;
import com.bsb.base.model.SysOrganizationExample.Criteria;
import com.bsb.biz.service.SysOrganizationService;
import com.bsb.biz.service.SysUserService;
import com.bsb.biz.shiro.UserUtils;



@Controller
@RequestMapping("/organization")
public class SysOrganizationController{
	
	
	@Resource
	private SysOrganizationService sysOrganizationService;
	@Resource
	private SysUserService sysUserService;
	
	@RequiresPermissions("organization:view")
    @RequestMapping(method = RequestMethod.GET)
    public String index(Model model) {
        return "organization/index";
    }
	
	@RequiresPermissions("organization:view")
    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    public String showTree(Model model) {
        SysOrganizationExample example = new SysOrganizationExample();
        Criteria criteria = example.createCriteria();
        if(Const.SUPER_ADMIN!=UserUtils.currentUser().getId()){
        	criteria.andIdIn(sysUserService.getDeptIdsByUser());
        	criteria.andAvailableEqualTo(true);
        }
        example.setOrderByClause("concat(parent_ids, id) asc");
		model.addAttribute("organizationList", sysOrganizationService.selectByExample(example));
        return "organization/tree";
    }

    @RequiresPermissions("organization:create")
    @RequestMapping(value = "/{parentId}/appendChild", method = RequestMethod.GET)
    public String showAppendChildForm(@PathVariable("parentId") Long parentId, Model model) {
        SysOrganization parent = sysOrganizationService.getSysOrganizationById(parentId);
        model.addAttribute("parent", parent);
        SysOrganization child = new SysOrganization();
        child.setParent_id(parentId);
        child.setParent_ids(parent.makeSelfAsParentIds());
        model.addAttribute("child", child);
        model.addAttribute("op", "新增");
        return "organization/appendChild";
    }

    @RequiresPermissions("organization:create")
    @RequestMapping(value = "/{parentId}/appendChild", method = RequestMethod.POST)
    public String create(SysOrganization organization) {
    	sysOrganizationService.addSysOrganization(organization);
        return "redirect:/organization/success";
    }

    @RequiresPermissions("organization:update")
    @RequestMapping(value = "/{id}/maintain", method = RequestMethod.GET)
    public String showMaintainForm(@PathVariable("id") Long id, Model model) {
        model.addAttribute("organization", sysOrganizationService.getSysOrganizationById(id));
        return "organization/maintain";
    }

    @RequiresPermissions("organization:update")
    @RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
    public String update(SysOrganization organization, RedirectAttributes redirectAttributes) {
    	sysOrganizationService.updateSysOrganization(organization);
        redirectAttributes.addFlashAttribute("msg", "修改成功");
        return "redirect:/organization/success";
    }

    @RequiresPermissions("organization:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
    public String delete(@PathVariable("id") Long id,Model model, RedirectAttributes redirectAttributes) {
    	//判断是否有子部门
		List<Long> deptList = sysOrganizationService.queryDetpIdList(id);
		if(deptList.size() > 0){
			redirectAttributes.addFlashAttribute("msg", "请先删除子部门");
			 model.addAttribute("organization", sysOrganizationService.getSysOrganizationById(id));
		     return "redirect:/organization/"+id+"/maintain";
		}
    	sysOrganizationService.delSysOrganizationById(id);
        redirectAttributes.addFlashAttribute("msg", "删除成功");
        return "redirect:/organization/success";
    }


    @RequiresPermissions("organization:update")
    @RequestMapping(value = "/{sourceId}/move", method = RequestMethod.GET)
    public String showMoveForm(@PathVariable("sourceId") Long sourceId, Model model) {
    	SysOrganization source = sysOrganizationService.getSysOrganizationById(sourceId);
        model.addAttribute("source", source);
        model.addAttribute("targetList", sysOrganizationService.findAllWithExclude(source));
        return "organization/move";
    }

    @RequiresPermissions("organization:update")
    @RequestMapping(value = "/{sourceId}/move", method = RequestMethod.POST)
    public String move(
            @PathVariable("sourceId") Long sourceId,
            @RequestParam("targetId") Long targetId) {
    	SysOrganization source = sysOrganizationService.getSysOrganizationById(sourceId);
    	SysOrganization target = sysOrganizationService.getSysOrganizationById(targetId);
    	sysOrganizationService.move(source, target);
        return "redirect:/organization/success";
    }

    @RequiresPermissions("organization:view")
    @RequestMapping(value = "/success", method = RequestMethod.GET)
    public String success() {
        return "organization/success";
    }


}
